//
//  HMCreatKittenCourse.m
//  开设三
//
//  Created by 陈辉明 on 2017/12/12.
//  Copyright © 2017年 goldwin. All rights reserved.
//

#import "HMCreatKittenCourse.h"
#import "TeacherCourseSelectModel.h"
#import "TeacherCourseSelectView.h"
#import "ZYCurriculumMajorCategoryView.h"
#import "ZYBigCourseThreeModel.h"
#import "HMSeletCommonView.h"
#import "HMGetTemplatesModel.h"

@interface HMCreatKittenCourse ()

@property (weak, nonatomic) IBOutlet UIButton *classNameButton;
@property (weak, nonatomic) IBOutlet UIButton *teacherNameButton;
@property (weak, nonatomic) IBOutlet UIButton *commonNameButton;
@property (nonatomic,strong) NSMutableArray *dataSourceThree;
@property(nonatomic,strong) NSMutableArray *classNameArray;
@property (nonatomic,strong) ZYBigCourseModel *model;
@property (nonatomic,strong) ZYBigCourseTowModel *modelTow;
@property (nonatomic,strong) ZYBigCourseThreeModel *modelThree;
@property (nonatomic,copy)NSString *ID;//模板ID
@property(nonatomic,copy) NSString *teacherId;//老师ID
@property(nonatomic,copy) NSString *packageId;//课程名称ID

//teacherId=255&templateId=55&packageId=208


@end

@implementation HMCreatKittenCourse

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"开设三好小当家课程";
}

//选择课程名称

- (IBAction)selectClassName:(UIButton *)sender {
    [self setNetWorkClassName];
    
}


//选择上课老师
- (IBAction)selectTeahcer:(UIButton *)sender {
    
    [self GoAddCourse];
}


//选择评论模板
- (IBAction)selectCommon:(id)sender {
    [self setNetWorkThree];
}



//确认
- (IBAction)clickSureButton:(id)sender {
    [self setSureNetWork];
}




#pragma mark - 获取老师列表数据

- (void)RefreshDataWithTeacher:(void (^)(NSArray <TeacherCourseSelectModel *>*Arr))Complete {
    
    [MBProgressHUD showHUD];
    NSDictionary *Params = @{
                             @"action":@"getTeacherByOid",
                             @"oid":[AccountTool shareAccountTool].currentAccount.oid
                             };
    [NetManager Get:LLBaseURL(@"lesson.do") Auth:YES Params:Params Success:^(id Obj) {
        
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            
            NSMutableArray *MutableArr = [NSMutableArray array];
            [Obj[@"t"] enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [MutableArr addObject:[TeacherCourseSelectModel HNObjectValuesForKey:obj]];
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                Complete ? Complete ([MutableArr copy]) : nil ;
            });
            
            
        } else {
            [MBProgressHUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        [MBProgressHUD showError:NetErr];
    }];
}

//选择老师弹框
- (void)GoAddCourse
{
    // 先获取老师的信息
    [self RefreshDataWithTeacher:^(NSArray<TeacherCourseSelectModel *> *Arr) {
        
        NSMutableArray *TitleArr = [NSMutableArray array];
        [Arr enumerateObjectsUsingBlock:^(TeacherCourseSelectModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [TitleArr addObject:obj.petName];
        }];
    
        // 弹窗并选择老师
        TeacherCourseSelectView *View = [TeacherCourseSelectView ConfigWithArr:[TitleArr copy] Complete:^(NSInteger Index) {
//             TitleArr[Index];
            [self.teacherNameButton setTitle:TitleArr[Index] forState:(UIControlStateNormal)];
            self.teacherId = [NSString stringWithFormat:@"%@",Arr[Index].uid];

        }];
        View.Title.text = @"请选择课程老师";
    }];
}


#pragma mark --- 模板接口
- (void)setNetWorkThree{
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=getTemplates"];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [MBProgressHUD showHUD];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [self.dataSourceThree removeAllObjects];
        [MBProgressHUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            
            NSArray *arr = [t  valueForKey:@"dataList"];
            
            for (NSDictionary *dict  in arr) {
                ZYBigCourseThreeModel *model = [ZYBigCourseThreeModel new];
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSourceThree addObject:model];
            }

            if (self.dataSourceThree.count != 0) {
                [self curriculumThree];
            }else{
                self.ID = @"0";
                [self createAlertViewWithTitle:nil message:@"没有数据" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
            }
            
           
            
        }
        
        
        
        
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
    }];
    
    
    
}

#pragma mark --- 模板方法的实现
- (void)curriculumThree{
    __block typeof(self) weakSelf  = self;
    [ZYCurriculumMajorCategoryView showWithTitle:@"请选择评价模板" titles:self.dataSourceThree zYBigCourseModel:self.model zYBigCourseTowModel:self.modelTow zYBigCourseThreeModel:self.modelThree selectIndex:^(NSInteger selectIndex) {
        NSLog(@"选择了第%ld个",selectIndex);
        weakSelf.ID = [NSString stringWithFormat:@"%ld",(long)selectIndex];
        
    } selectValue:^(NSString *selectValue) {
        NSLog(@"选择的值为%@",selectValue);
        [weakSelf.commonNameButton setTitle:selectValue forState:(UIControlStateNormal)];
        
    } showCloseButton:NO  isUp:NO isUpAll:YES];
    
    
    
}




- (void)setNetWorkClassName{
       
    //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=getLittleManByLocation
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=getLittleManByLocation"];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [MBProgressHUD showHUD];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
     
        [MBProgressHUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            [self.classNameArray removeAllObjects];
            for (NSDictionary *dict in Obj[@"t"][@"dataList"]) {
                HMGetTemplatesModel *model = [HMGetTemplatesModel HMGetTemplatesModelWithDict:dict];

                [self.classNameArray addObject:model];
            }
            
            if (self.classNameArray.count == 0) {
                [self createAlertViewWithTitle:nil message:@"没有数据" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
            } else {
                [self creatClasViewWithArray:self.classNameArray];
            }
            
           
        }
        
        
        
        
        
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
    }];
    
    
    
}

#pragma mark --- 课程名称的实现
- (void)creatClasViewWithArray: (NSMutableArray *)array{
    
    
    HMSeletCommonView *view = [[HMSeletCommonView alloc]initHMSeletCommonViewWithArr:array];

    __block typeof(self) weakSelf  = self;
    view.selectCommonBlock = ^(HMGetTemplatesModel *model) {
        [weakSelf.classNameButton setTitle:model.name forState:(UIControlStateNormal)];
        weakSelf.packageId = [NSString stringWithFormat:@"%@",model.Id];
    };
    
    
}


-(void)setSureNetWork {
    if ([self.classNameButton.titleLabel.text isEqualToString:@"请选择"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择课程名称"];
        return;
    } else if([self.teacherNameButton.titleLabel.text isEqualToString:@"请选择"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择上课老师"];
        return;
    } else if([self.commonNameButton.titleLabel.text isEqualToString:@"请选择"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择评论模板"];
        return;
    } else {
        
        //http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=saveLittleManByOrg&teacherId=255&templateId=55&packageId=208
        NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=saveLittleManByOrg&teacherId=%@&templateId=%@&packageId=%@",self.teacherId,self.ID,self.packageId];
        NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
        [MBProgressHUD showHUD];
        [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {


            if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
                [MBProgressHUD showSuccess:@"添加成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     [self.navigationController popViewControllerAnimated:YES];
                     if (self.sureBolck) {
                        self.sureBolck();
                        
                     }
                });
                
                
                
            } else {
                [MBProgressHUD showError:@"添加失败,请重新尝试"];
            }
            

        } Fail:^(NSError *Error) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:NetErr];
        }];

    }
    
    
    
    
    
    
    
}



- (NSMutableArray *)dataSourceThree {
    if (!_dataSourceThree) {
        _dataSourceThree = [NSMutableArray array];
    }
    return _dataSourceThree;
}

-(NSMutableArray *)classNameArray {
    if (!_classNameArray) {
        _classNameArray = [NSMutableArray array];
    }
    return _classNameArray;
}

@end
